/**
 * Created by nitin on 3/24/14.
 */

var appModule = angular.module('application', ['angularFileUpload','ngRoute','ngAnimate']);
//appModule.run(function($templateCache) {
//    $templateCache.put('helloTemplateCached.txt', '<div>Hi there as</div>');
//});
var BACK_END_URL = "http://www.nitin.bugs3.com/service/";
//var BACK_END_URL = "http://localhost/nihost/service/";
var BACK_END_BOM = BACK_END_URL + "bom/";
var BACK_END_BACK = BACK_END_URL + "back/";
appModule.directive('headpage', function () {
    return {
        restrict: 'E',
        templateUrl: 'temp/headPage.txt',
        replace: true
    };
});
appModule.directive('footpage', function () {
    return {
        restrict: 'E',
        templateUrl: 'temp/footPage.txt',
        replace: true
    };
});
appModule.directive('menupage', function () {
    return {
        restrict: 'E',
        templateUrl: 'temp/menuPage.txt',
        replace: true
    };
});
appModule.directive('sociallink', function () {
    return {
        restrict: 'E',
        templateUrl: 'temp/sociallink.txt',
        replace: true
    };
});
appModule.directive('list', function () {
    return {
        restrict: 'E',
        templateUrl: 'temp/list.txt',
        replace: true
    };
});
appModule.directive('cover', function () {
    return {
        restrict: 'E',
        templateUrl: 'temp/cover.txt',
        replace: true
    };
});
appModule.directive('switcher', function () {
    return {
        restrict: 'E',
        templateUrl: 'temp/switcher.txt',
        replace: true
    };
});
appModule.directive('grid', function () {
    return {
        restrict: 'E',
        templateUrl: 'temp/grid.txt',
        replace: true
    };
});
appModule.directive('gridselect', function () {
    return {
        restrict: 'E',
        templateUrl: 'temp/gridSelect.txt',
        replace: true
    };
});
appModule.directive('fileupload', function () {
    return {
        restrict: 'E',
        templateUrl: 'temp/fileupload.html',
        replace: true
    };
});
appModule.directive('tablepage', function () {
    return {
        restrict: 'E',
        templateUrl: 'temp/tablePage.html',
        replace: true
    };
});
appModule.directive('datePicker', function () {
    return {
        restrict: 'E',
        templateUrl: 'temp/datePicker.html',
        replace: true
    };
});
appModule.directive('message', function () {
    return {
        restrict: 'E',
        templateUrl: 'temp/message.html',
        replace: true
    };
});
'use strict';
appModule.directive('ngThumb', ['$window', function ($window) {
    var helper = {
        support: !!($window.FileReader && $window.CanvasRenderingContext2D),
        isFile: function (item) {
            return angular.isObject(item) && item instanceof $window.File;
        },
        isFileUrl: function (item) {
            return item.src;
        },
        isImage: function (file) {
            var type = '|' + file.type.slice(file.type.lastIndexOf('/') + 1) + '|';
            return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
        }
    };
    return {
        restrict: 'A',
        template: '<canvas/>',
        link: function (scope, element, attributes) {
            if (!helper.support) return;

            var params = scope.$eval(attributes.ngThumb);
            var canvas = element.find('canvas');
            var reader = new FileReader();

            if (helper.isFileUrl(params.file)) {
                element.parent().parent().parent().addClass("waiting");
                var img = new Image();
                img.onload = function onLoadImage(e) {

                    if (this.height > this.width) {
                        var height = params.height > this.height ? this.height : params.height;
                        var width = this.width / this.height * height;
                    } else {
                        var width = params.width > this.width ? this.width : params.width;
                        var height = this.height / this.width * width;
                    }
                    canvas.attr({ width: width, height: height });
                    canvas[0].getContext('2d').drawImage(this, 0, 0, width, height);
                    element.parent().parent().parent().removeClass("waiting");
                }
                img.src = params.file.src;
                return;
            }
            if (!helper.isFile(params.file)) return;
            if (!helper.isImage(params.file)) return;


            reader.onload = onLoadFile;
            reader.readAsDataURL(params.file);

            function onLoadFile(event) {
                var img = new Image();
                img.onload = onLoadImage;
                img.src = event.target.result;
            }

            function onLoadImage() {
                if (this.height > this.width) {
                    var height = params.height > this.height ? this.height : params.height;
                    var width = this.width / this.height * height;
                } else {
                    var width = params.width > this.width ? this.width : params.width;
                    var height = this.height / this.width * width;
                }
                canvas.attr({ width: width, height: height });
                canvas[0].getContext('2d').drawImage(this, 0, 0, width, height);
            }
        }
    };
}]);

/*

 appModule.config(['$routeProvider', function($routeProvider) {
 $routeProvider.
 when('/', {
 controller: 'ListCtrl',
 templateUrl:'/views/list.txt'
 }).

 when('/product/:productId', {
 controller: 'NewCtrl',
 templateUrl:'/views/recipeForm.txt'
 })
 .otherwise({redirectTo:'/'});

 }]);*/
